package com.apes.fn.scm.purchaseReturn.model;

import com.apes.fn.tms.model.Site;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.scm.purchaseReturn.model
 * @ClassName: PurchaseAfterJunior
 * @Author: ljp
 * @CreateTime: 2021-08-06 15:09
 * @Description: 售后工单
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PurchaseAfterJunior.CONST_TABLE_NAME)
public class PurchaseAfterJunior extends Model {
    public static final String CONST_TABLE_NAME = "purchase_after_junior";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "售后工单", padding = 8, useDateRange=true)
    private String id;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "location_id", referencedColumnName = "id")
    @Comment("仓库")
    private Location location;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "site", referencedColumnName = "id")
    @Comment("售后")
    private Site site;

    @Column(name = "note")
    @Comment("备注")
    private String note;

    public void setState(String state) {
        if ("done".equals(state)&&this.note!=null&&this.note.contains("待财务审核")) {
            String[] wait = this.note.split("待财务审核");
            StringBuffer sb = new StringBuffer();
            for (String s : wait) {
                sb.append(s);
            }
            this.note=sb.toString();
        };
        this.state = state;
    }

    @Column(name = "state", length = 10, nullable = false)
    @States(name = "订单状态", values = {
            @State(id = "apply", name = "申请"),
            @State(id = "pending", name = "待处理"),
            @State(id = "deliver", name = "待发货"),
            @State(id = "receipt", name = "待收货"),
            @State(id = "backDeli", name = "待退回发货"),
            @State(id = "backRece", name = "待退回收货"),
            @State(id = "revert", name = "待返厂发货"),
            @State(id = "identify", name = "待鉴定"),
            @State(id = "finance", name = "财务审核"),
            @State(id = "done", name = "完成")})
    private String state;

    @Column(name = "source", length = 30)
    @Comment("售后单号")
    private String source;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "create_uid", referencedColumnName = "id")
    @Comment("创建人")
    private PartyRole createUid;

    @Column(name = "create_date")
    @Comment("创建时间")
    private Date createDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "reject_uid", referencedColumnName = "id")
    @Comment("驳回人")
    private PartyRole rejectUid;


    @Column(name = "reject_date")
    @Comment("驳回日期")
    private Date rejectDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "consent_uid", referencedColumnName = "id")
    @Comment("同意人")
    private PartyRole consentUid;

    @Column(name = "consent_date")
    @Comment("同意日期")
    private Date consentDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "back_uid", referencedColumnName = "id")
    @Comment("退回人")
    private PartyRole backUid;

    @Column(name = "back_date")
    @Comment("退回日期")
    private Date backDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "repeal_uid", referencedColumnName = "id")
    @Comment("撤销人")
    private PartyRole repealUid;

    @Column(name = "repeal_date")
    @Comment("撤销日期")
    private Date repealDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "apply_uid", referencedColumnName = "id")
    @Comment("申请人")
    private PartyRole applyUid;

    @Column(name = "apply_date")
    @Comment("申请日期")
    private Date applyDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUid;

    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "refuse_uid", referencedColumnName = "id")
    @Comment("拒绝人")
    private PartyRole refuseUid;

    @Column(name = "refuse_date")
    @Comment("拒绝日期")
    private Date refuseDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "return_reason", referencedColumnName = "id")
    @Comment("返厂原因")
    private Enumeration returnReason;

    @OneToMany(mappedBy = "purchaseAfterJunior", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<PurchaseAfterJuniorItem> purchaseAfterJuniorItems = new HashSet<>();

    public Set<PurchaseAfterJuniorItem> getPurchaseAfterJuniorItems() {
        for (PurchaseAfterJuniorItem item : purchaseAfterJuniorItems) {
            item.setPurchaseAfterJunior(this);
        }
        return purchaseAfterJuniorItems;
    }

    @Transient
    private PartyRole supplier;

    public PartyRole getSupplier() {
        PartyRole s = new PartyRole();
        for (PurchaseAfterJuniorItem purchaseAfterJuniorItem : getPurchaseAfterJuniorItems()) {
            return s =  purchaseAfterJuniorItem.getSupplier();
        }
        return s;
    }
}
